package YunNotes.servlet;

import YunNotes.allnotes.control.NoteAction;
import YunNotes.util.json.Del;
import YunNotes.util.json.GetJson;
import YunNotes.util.json.JsonChange;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;


/**
 * @author Hzheng
 * @date 2020-05-15 20:11
 */
@WebServlet(name = "NotesServlet")
public class NotesServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();
        out.println("前端的请求方法是：" + request.getMethod());

        //接收前端数据
        InputStreamReader insr = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);
        StringBuilder result = new StringBuilder();
        int respInt = insr.read();
        while (respInt != -1) {
            // 读取请求数据
            result.append((char) respInt);
            respInt = insr.read();
        }
        insr.close();
        String data = result.toString();

        //JSON->对象,并且应用到数据库
        JsonChange c = new JsonChange();
        NoteAction noteAction = new NoteAction();

        try {
            noteAction.add(c.StringtoJson(data));
            out.println("笔记添加成功！但未设置分类！");
        } catch (SQLException e) {
            out.println("添加失败！");
            e.printStackTrace();
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");

        //给前端输出
        PrintWriter out = response.getWriter();
        out.println("前端的请求方法是：" + request.getMethod());

        //获取id的值
        String s = request.getParameter("id");
        int id = Integer.parseInt(s);

        //应用到数据库
        NoteAction noteAction = new NoteAction();
        JsonChange jsonChang = new JsonChange();
        Del del = new Del();
        try {
            out.println(del.delToJson(jsonChang.ListToJson(noteAction.query(id))));
        } catch (SQLException e) {
            out.println("查询失败！");
            e.printStackTrace();
        }
    }


    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //给前端输出
        PrintWriter out = response.getWriter();
        out.println("前端的请求方法是：" + request.getMethod());

        //接收前端数据
        InputStreamReader insr = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);
        StringBuilder result = new StringBuilder();
        int respInt = insr.read();
        while (respInt != -1) {
            // 读取请求数据
            result.append((char) respInt);
            respInt = insr.read();
        }
        System.out.println("前端传递的数据为:" + result);
        insr.close();
        String data = result.toString();

        //获取num和action的值
        GetJson getJson = new GetJson();
        String s1 = getJson.Get(data, "num");
        String s2 = getJson.Get(data, "action");
        int num = Integer.parseInt(s1);
        int action = Integer.parseInt(s2);

        //应用到数据库
        NoteAction noteAction = new NoteAction();

        if (action == 1) {
            String s3 = getJson.Get(data, "headline");
            try {
                noteAction.updateHeadline(s3, num);
                out.println("修改num为：" + num + "的标题成功！");
            } catch (SQLException e) {
                out.println("修改标题失败！");
                e.printStackTrace();
            }
        } else if (action == 2) {
            String s3 = getJson.Get(data, "content");
            try {
                noteAction.upContent(s3, num);
                out.println("修改num为：" + num + "的内容成功！");
            } catch (SQLException e) {
                out.println("修改内容失败！");
                e.printStackTrace();
            }
        } else {
            out.println("修改失败，publicity 的值不是2也不是3");
        }
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //给前端输出
        PrintWriter out = response.getWriter();
        out.println("前端的请求方法是：" + request.getMethod());

        //获取num的值
        String s = request.getParameter("num");
        int num = Integer.parseInt(s);

        //应用到数据库
        NoteAction noteAction = new NoteAction();
        try {
            noteAction.delNote(num);
            out.println("成功删除num：" + num + "的笔记！");
        } catch (SQLException e) {
            out.println("删除失败！");
            e.printStackTrace();
        }
    }
}


